﻿LOGIN = function () {

}
LOGIN.arrays = new Array();
LOGIN.fieldText = new Array("password", "newPass", "Repass");
LOGIN.Init = function () {
    LOGIN.arrays = new Array();
    var obj;
    obj = new CheckField("password", 1, "Errpassword");
    LOGIN.Add(obj);
    obj = new CheckField("newPass", 1, "ErrnewPass");
    LOGIN.Add(obj);
    obj = new CheckField("Repass", 1, "ErrRepass");
    LOGIN.Add(obj);
}

LOGIN.Add = function (obj) {
    if (LOGIN.arrays.contains(obj) == false) {
        LOGIN.arrays.push(obj);
    }
}

LOGIN.Login = function () {
    var userName = $("#username").val();
    var pass = $("#password").val();
    var serviceUrl = Constants.service + "logIn";
    var data = {
        username: userName,
        password: pass
    };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var result = msg.logInResult;
            if (result != null && result != undefined) {
                if (parseInt(result.key) >= 1) {
                    var s_data = {
                        username: userName,
                        password: result.password,
                        key: result.key
                    }
                    localStorage.setItem('adminLocalStorage', JSON.stringify(s_data));
                    var url = Constants.Url + "admin/List.aspx";
                    window.open(url, "_self");
                }
                else if (parseInt(result.key) == -1) {
                    alert("Sai mat khau");
                    $("#password").focus();
                }
                else {
                    alert("Tên đăng nhập không tồn tại");
                    $("#username").focus();
                }
            }
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

LOGIN.ChangPass = function () {
    var serviceUrl = Constants.service + "changePassWordAdmin";
    var username = "";
    var password = $("#password").val();
    var local_storage = JSON.parse(localStorage.getItem('adminLocalStorage'));
    username = local_storage.username;

    var newPass = $("#newPass").val();
    var rePass = $("#Repass").val();
    if (Constants.OnSubmit(LOGIN.arrays) == false) {
        return false;
    }
    var data = {
        username: username,
        password: password,
        newPass: newPass
    };

    if (newPass != rePass) {
        $("#ErrRepass").html("Mật khẩu không khớp");
        return;
    }
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var result = msg.changePassWordAdminResult;
            if (parseInt(result, 10) == 1) {
                alert("Thay đổi mật khẩu thành công");
            }
            else {
                $("#Errpassword").html("Mật khẩu nhập chưa đúng");
            }
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

LOGIN.EnterChanged = function () {
    $("#password").keypress(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            $("#newPass").focus();
        }
    });
    $("#newPass").keypress(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            $("#Repass").focus();
        }
    });
    $("#Repass").keypress(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            LOGIN.ChangPass();
        }
    });
}
